<?php

if (!class_exists("cmsTypeConf")) {

/**
 * Class cmsTypeConf encapsulates the type configurations and provides methods to handle them.
 *
 *
 */
    class cmsTypeConf {

        var $imagefields; // Felder von Bildern in einer Contentklasse
        var $linkfields;  // Felder von Links in einer Contentklasse
        var $textfields;  // Felder von Texten in einer Contentklasse
        var $framefields; // Felder von Frames in einer Contentklasse
        var $bulletlistfields; // Felder von Bulletlists einer Contentklasse
        /**
         * Constructor: splits the string with the configuration and stores it in a format the methods need to act on it.
         *
         * @param Array $tcarray Array which comes from dbaCMS->getTypes() method.
         */
        function cmsTypeConf ($tcarray) {
            for ($j=0;$j<count($tcarray);$j++) {
                $EditTypes  = explode(';',$tcarray[$j]['conf']);
                $splitTypes = array();
                for ($i=0;$i<count($EditTypes);$i++) {
                    $EditType    = explode(':',$EditTypes[$i]);
                    $splitTypes[] = $EditType;
                }
                $this->confs[$tcarray[$j]['name']] = $splitTypes;
                $splitTypes    = array();
            }
        }

        /**
         * Checks if an image file is linked in a document
         *
         * @return types array
         */
        function hasImageLinks(& $udba, $id) {
            $types = array_keys($this->confs);
            $selects = array();
            foreach ($types as $t) {
                $imgf = $this->getImageFields($t);
                if (count($imgf) > 0) {
                    $sql = "select id, element_id from ".$udba->tableforwarder."_cmsType_$t where ";
                    for ($i = 0; $i < count($imgf); $i++) {
                        $imgf[$i] = $imgf[$i]." = 'imageid:$id'";
                    }
                    $sql .= join(' or ', $imgf);
                    $selects[] = $sql;
                }
            }
            $union = join(' union ', $selects);
            $sth = $udba->query($union);
            return (count($sth->fetch_rows()) > 0);
        }

        /**
         * Retrieves the documents where the image is used
         *
         * @return array doc_ids
         */
        function getDocsIdsByImage(& $udba, $id) {
            $types = array_keys($this->confs);
            $selects = array();
            foreach ($types as $t) {
                $imgf = $this->getImageFields($t);
                if (count($imgf) > 0) {
                    $sql = "select I.doc_id from ".$udba->tableforwarder."_cmsType_$t T, ".$udba->tableforwarder."_cmsElement E, ".$udba->tableforwarder."_cmsElementInstance I where ";
                    for ($i = 0; $i < count($imgf); $i++) {
                        $imgf[$i] = $imgf[$i]." = 'imageid:$id'";
                    }
                    $sql .= join(' or ', $imgf);
                    $selects[] = $sql." and T.element_id = E.element_id and I.element_id = E.element_id";
                }
            }
            $union = join(' union ', $selects);
            $sth = $udba->query($union);
            $rows = $sth->fetch_rows();
            $rrows = array();
            for ($i = 0; $i < count($rows); $i++) {
                if (!in_array($rows[$i][0], $rrows)) {
                    $rrows[] = $rows[$i][0];
                }
            }
            return $rrows;
        }

        /**
         * Returns the document ids the image is linked in.
         *
         * @return types array
         */
        function getImageLinkedDocuments(& $udba, $id) {
        // FIXME: implement
        }


        function getTextFields ($type) {
            if (empty($this->textfields[$type])) {
                $t = &$this->confs[$type];
                $fields = null;
                for ($i=0;$i<count($t);$i++) {
                    if (($t[$i][2]=='text') || ($t[$i][2]=='textField') || ($t[$i][2]=='shortText'))
                        $fields[] = $t[$i][0];
                }
                $this->textfields[$type] = $fields;
            }
            return $this->textfields[$type];
        }

        /**
         * Adds the word count prefix_wc and the character count prefix_cc
         *
         * Is needed by templateAnalyser to initialize the word and character count
         *
         * @param string $type the types identifier/name
         */
        function calcWordCount ($type, & $data) {
            $this->getTextFields($type);
            if (is_array($this->textfields[$type])) {
                for ($i = 0; $i < count($this->textfields[$type]); $i++) {
                    $stripped = strip_tags($data[$this->textfields[$type][$i].'text']);
                    $data[$this->textfields[$type][$i].'wc'] = str_word_count($stripped);
                    $data[$this->textfields[$type][$i].'cc'] = strlen($stripped);
                }
            }
        }

        /**
         * Finds and returns the fields of a type which contain a link's href.
         *
         * Is needed by templateAnalyser to find the internal links which it must make absolute links.
         *
         * @param string $type the types identifier/name
         */
        function getLinkFields ($type) {
            $fields = array();
            if (empty($this->linkfields[$type])) {
                $t = &$this->confs[$type];
                for ($i=0;$i<count($t);$i++) {
                    if (($t[$i][2]=='link') || ($t[$i][2]=='targetLink')) $fields[] = $t[$i][0].'href';
                }
                $this->linkfields[$type] = $fields;
            }
            if (isset($this->linkfields[$type]))
                return $this->linkfields[$type];
        }

        /**
         * Finds and returns the fields of a type which contain a bulletlist.
         *
         * Is needed by templateAnalyser to find the bulletlists and process them
         *
         * @param string $type the types identifier/name
         */
        function getBulletListFields ($type) {
            $fields = array();
            if (empty($this->bulletlistfields[$type])) {
                $t = &$this->confs[$type];
                for ($i=0;$i<count($t);$i++) {
                    if ($t[$i][2]=='bulletlist') $fields[] = $t[$i][0];
                }
                $this->bulletlistfields[$type] = $fields;
            }
            if (isset($this->bulletlistfields[$type]))
                return $this->bulletlistfields[$type];
        }

        /**
         * Finds and returns the fields of a type which contain a frame's src.
         *
         * Is needed by templateAnalyser to find the internal frames references which it must make absolute references.
         *
         * @param string $type the types identifier/name
         */
        function getFrameFields ($type) {
            $fields = array();
            if (empty($this->framefields[$type])) {
                $t = &$this->confs[$type];
                for ($i=0;$i<count($t);$i++) {
                    if ($t[$i][2]=='frame') $fields[] = $t[$i][0].'src';
                }
                $this->framefields[$type] = $fields;
            }
            if (isset($this->framefields[$type]))
                return $this->framefields[$type];
        }

        /**
         * Finds and returns the fields of a type which contains an image's href.
         *
         * Is needed by templateAnalyser to find the internal image references which it must make absolute urls.
         *
         * @param string $type the types identifier/name
         */
        function getImageFields ($type) {
            $fields = array();
            if (empty($this->imagefields[$type]) || !is_array($this->imagefields[$type])) {
                $t = &$this->confs[$type];
                for ($i=0;$i<count($t);$i++) {
                    if ($t[$i][2]=='image') $fields[] = $t[$i][0].'href';
                }
                $this->imagefields[$type] = $fields;
            }
            if (isset($this->imagefields[$type]))
                return $this->imagefields[$type];
        }

        function getConfArray ($name) {
            if (isset($this->confs[$name]))
                return $this->confs[$name];
        }

    }

}
?>